<?php
class user extends spController
{
	function index()
	{
		
	}
	
	function login()
	{
		if ( $this->spArgs('submit') ) {
			$membersObj = spClass("cms_members");
			$membersObj->verifier = $membersObj->verifier_login;
			$verifier = $membersObj->spVerifier( $this->spArgs() );
			if( false == $verifier ) {
				
				// 查询用户信息
				$whereArr = array('username'=>$this->spArgs('username'));
				$result = $membersObj->find( $whereArr );
				
				// 验证用户信息
				if ($result && $result['password'] == md5($this->spArgs('password'))) {
					
					// 设置访问权限
					spClass('spAcl')->set( $result['acl'] );
					
					// 设置Cookie
					if ( $this->spArgs('autologin') ) {
						setcookie("uid", $result['uid'], time()+604800, '/', '.qiaoshouji.com');
						setcookie("username", $result['username'], time()+604800, '/', '.qiaoshouji.com');
					} else {
						setcookie("uid", $result['uid'], time()+3600, '/', '.qiaoshouji.com');
						setcookie("username", $result['username'], time()+3600, '/', '.qiaoshouji.com');
					}
					
					// 更新用户登录时间和登录次数
					$updateArr = array('lastlogin'=>time(), 'loginum'=>$result['loginum']+1);
					$membersObj->update(array('uid'=>$result['uid']), $updateArr);
					
					// 返回AJAX结果
					$ajaxResult = array('status' => 1, 'message' => '登录成功');
				} else {
					$ajaxResult = array('status' => 0, 'message' => '用户信息不正确');
				}
			} else {
				$ajaxResult = array('status' => 0, 'message' => '登录信息格式不正确');
			}
			echo json_encode( $ajaxResult );
		} else {
			$this->display("login.html");
		}
	}
	
	function logout()
	{
		setcookie('uid', '', -1, '/', '.qiaoshouji.com');
		setcookie('username', '', -1, '/', '.qiaoshouji.com');
		spClass('spAcl')->set('');
		$this->jump(spUrl('main', 'index')); 
	}
	
	function reg()
	{
		if ($this->spArgs('submit')) {
			$addArr = array(
				'username' => trim( $this->spArgs('username') ),
				'password' => md5(trim( $this->spArgs('password')) ),
				'email'    => trim( $this->spArgs('email') ),
				'regtime'  => time(),
				'acl' => 'USER'
			);
			$membersObj = spClass("cms_members");
			$membersObj->verifier = $membersObj->verifier_register;
			$verifier = $membersObj->spVerifier( $this->spArgs() );
			if( false == $verifier && false == $this->checkname(false) && false == $this->checkemail(false)){
				$uid = $membersObj->create( $addArr );
				if ($uid) {
					$ajaxResult = array('status' => 1, 'message' => '注册成功');
				} else {
					$ajaxResult = array('status' => 0, 'message' => '注册失败');
				}
			} else {
				$ajaxResult = array('status' => 0, 'message' => '注册失败');
			}
			echo json_encode( $ajaxResult );
		} else {
			$this->display("register.html");
		}
	}
	
	function checkname($ajax=true)
	{
		$membersObj = spClass("cms_members");
		$result = $membersObj->find( array('username'=>$this->spArgs('username')) );
		if ($ajax == true) {
			if ($result) {
				$ajaxResult = array('status' => 0, 'message' => '<font color="#FF0000">已被占用，请更换用户名</font>');
			} else {
				$ajaxResult = array('status' => 1, 'message' => '可以使用');
			}
			echo json_encode( $ajaxResult );
		} else {
			if ($result) {
				return true;
			} else {
				return false;
			}
		}
	}
	
	function checkemail($ajax=true)
	{
		$membersObj = spClass("cms_members");
		$result = $membersObj->find( array('email'=>$this->spArgs('email')) );
		if ($ajax == true) {
			if ($result) {
				$ajaxResult = array('status' => 0, 'message' => '<font color="#FF0000">已被占用，请更换邮箱</font>');
			} else {
				$ajaxResult = array('status' => 1, 'message' => '可以使用');
			}
			echo json_encode( $ajaxResult );
		} else {
			if ($result) {
				return true;
			} else {
				return false;
			}
		}
	}
}